package com.nut.lmsc.api.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.api.req.CancelOrderReq;
import com.nut.lmsc.api.req.OrderQueryReq;
import com.nut.lmsc.api.req.OrderReq;
import com.nut.lmsc.api.resp.SubmitOrderResp;
import com.nut.lmsc.common.Holder;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.Order;
import com.nut.lmsc.interceptor.ClientJwt;
import com.nut.lmsc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/client/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "list")
    public Result<IPage<Order>> list(OrderQueryReq req) {
        Long memberId = Holder.CLIENT_JWT_THREAD_LOCAL.get().getMemberId();
        req.setMemberId(memberId);
        return orderService.listPage(req);
    }

    /**
     * 订单详情
     */
    @GetMapping(value = "/{orderId}")
    public Result<Order> detail(@PathVariable String orderId) {
        return orderService.detail(orderId);
    }

    @GetMapping(value = "/status/{orderId}")
    public Result queryStatus(@PathVariable String orderId) {
        return orderService.queryStatus(orderId);
    }

    @GetMapping(value = "/money/{orderId}")
    public Result queryMoney(@PathVariable String orderId) {
        return orderService.queryMoney(orderId);
    }

    /**
     * 提交订单
     * @param orderReq
     * @return
     */
    @PostMapping(value = "submit")
    public Result<SubmitOrderResp> submit(@RequestBody OrderReq orderReq) {
        
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        orderReq.setMemberId(clientJwt.getMemberId());
        orderReq.setMobile(clientJwt.getMobile());

        return orderService.submit(orderReq);
    }

    @PostMapping(value = "/cancel")
    public Result<Order> cancel(@RequestBody CancelOrderReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();

        req.setUsername(clientJwt.getMobile());
        req.setMemberId(clientJwt.getMemberId());
        req.setFroms("客户端");

        return orderService.cancel(req);
    }

}